package cc.yiueil.service.impl;

import cc.yiueil.service.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private static final Map<String, Map<String, Object>> users;

    static {
        users = new HashMap<>();
        HashMap<String, Object> admin = new HashMap<>();
        admin.put("username", "admin");
        admin.put("password", "$2a$10$nOubKDnM2FLOsvFh1WPMGuj9ut06TkMhZ4ts13b14q.Snts5/xnYm");
        users.put("admin", admin);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Map<String, Object> user = users.get(username);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'", username));
        }
        return User.builder()
                .username(String.valueOf(user.get("username")))
                .password(user.get("password").toString())
                .authorities(AuthorityUtils.NO_AUTHORITIES)
                .roles("ADMIN").build();
    }
}
